package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GreLandRecordDao;
import cn.topevery.garden.entity.po.GreLand;
import cn.topevery.garden.entity.po.GreLandRecord;
import cn.topevery.garden.entity.query.GreLandRecordQuery;
import cn.topevery.garden.entity.vo.GreLandRecordVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbCommand;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 绿地记录
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
@Slf4j
public class GreLandRecordDaoImpl extends BaseDaoImpl<GreLandRecord> implements GreLandRecordDao {
    @Override
    public <P extends DbCsp> List<GreLandRecord> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<GreLandRecordVO> listPage(GreLandRecordQuery query) {
        String table = "t_gre_land_record tglr";

        String columns = "tglr.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tglr.c_id desc");
        sqlQueryMate
                .where(true, "tglr.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GreLandRecordVO.class);
    }

    @Override
    public GreLandRecord getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public int insert(GreLand greLand) {
        log.info("----------将绿地信息存入绿地信息记录表------------");
        GreLandRecord greLandRecord = new GreLandRecord();
        //将旧的记录修改为历史版本
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_gre_land_record");
        sqlUpdateMate.setColumnValue(true, "c_is_new", 0)
                .where(true, "c_land_int_id", ScriptConditionEnum.Eq, greLand.getId());
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
        BeanUtils.copyProperties(greLand, greLandRecord, "id");
        greLandRecord.setLandIntId(greLand.getId());
        greLandRecord.setIsNew(1);
        //存入新的记录
        insertBySql(greLandRecord);
        return greLandRecord.getId();
    }

    private void insertBySql(GreLandRecord greLand) {
        String sql = "INSERT INTO t_gre_land_record (\n" +
                "        c_land_int_id,\n" +
                "        c_land_id,\n" +
                "        c_code,\n" +
                "        c_big_class_id,\n" +
                "        c_big_class_name,\n" +
                "        c_medium_class_id,\n" +
                "        c_medium_class_name,\n" +
                "        c_small_class_id,\n" +
                "        c_small_class_name,\n" +
                "        c_alone_load,\n" +
                "        c_gree_class,\n" +
                "        c_type,\n" +
                "        c_measure,\n" +
                "        c_gre_measure,\n" +
                "        c_address,\n" +
                "        c_map_ranges,\n" +
                "        c_geo_wkt,\n" +
                "        c_service_radius,\n" +
                "        c_street_id,\n" +
                "        c_street_name,\n" +
                "        c_distract_id,\n" +
                "        c_distract_name,\n" +
                "        c_dept_id,\n" +
                "        c_desc,\n" +
                "        c_date,\n" +
                "        c_attr,\n" +
                "        c_attr_cnt,\n" +
                "        c_is_free,\n" +
                "        c_db_create_id,\n" +
                "        c_db_create_date,\n" +
                "        c_db_status,\n" +
                "        c_is_new,\n" +
                "        c_data_public\n" +
                "        )\n" +
                "        VALUES\n" +
                "        (\n" +
                greLand.getLandIntId() + "        ,\n" +
                greLand.getLandId() + "        ,\n" +
                greLand.getCode() + "        ,\n" +
                greLand.getBigClassId() + "        ,\n" +
                greLand.getBigClassName() + "        ,\n" +
                greLand.getMediumClassId() + "        ,\n" +
                greLand.getMediumClassName() + "        ,\n" +
                greLand.getSmallClassId() + "        ,\n" +
                greLand.getSmallClassName() + "        ,\n" +
                greLand.getAloneLoad() + "        ,\n" +
                greLand.getGreeClass() + "        ,\n" +
                greLand.getType() + "        ,\n" +
                greLand.getMeasure() + "        ,\n" +
                greLand.getGreMeasure() + "        ,\n" +
                greLand.getAddress() + "        ,";
        if (StringUtils.isNotBlank(greLand.getGeoWkt())) {
            sql += "ST_GeometryFromText(#{greLand.geoWkt}),";
        } else {
            sql += "null ,";
        }
        sql += greLand.getGeoWkt();
        if (StringUtils.isNotBlank(greLand.getServiceRadiusWkt())) {
            sql += "ST_GeometryFromText(" + greLand.getServiceRadiusWkt() + "),";
        } else {
            sql += "null ,";
        }
        sql += greLand.getStreetId() + ",\n" +
               greLand.getStreetName() + ",\n" +
               greLand.getDistractId() + ",\n" +
               greLand.getDistractName() + ",\n" +
               greLand.getDeptId() + ",\n" +
               greLand.getDesc() + ",\n" +
               greLand.getDate() + ",\n" +
               greLand.getAttr() + ",\n" +
               greLand.getAttrCnt() + ",\n" +
               greLand.getIsFree() + ",\n" +
               greLand.getCreateId() + ",\n" +
               greLand.getCreateTime() + ",\n" +
               greLand.getDbStatus() + ",\n" +
               greLand.getIsNew() + ",\n" +
               greLand.getDataPublic() + "}\n" +
               ")";
        run(sql, CommandType.Text, DbCommand::executeNonQuery);
    }
}
